package com.oren.activity.ctrl;

import com.oren.activity.pojo.ActDeployment;
import com.oren.activity.pojo.ProcessOwn;
import com.oren.activity.pojo.QueryTask;
import com.oren.activity.service.ActDeploymentService;
import com.oren.activity.service.ProcessOwnService;
import com.oren.base.ctrl.BaseController;
import com.oren.base.pojo.Page;
import com.oren.util.DataUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * @Author: wsw
 * @Description:
 * @Date: Create on 2018/8/1 20:47
 */
@Controller
@RequestMapping("/processOwn")
public class ProcessOwnController extends BaseController {

    @Autowired
    private ProcessOwnService processOwnService;

    @Autowired
    private ActDeploymentService actDeploymentService;

    @RequestMapping("/pageList")
    @ResponseBody
    public Page<ProcessOwn> pageList(HttpServletRequest request,ProcessOwn processOwn, int page, int rows){
        String userName=getSessionUserName(request);
        String userId=getSessionUserId(request);
        processOwn.setUserId(userId);
        return processOwnService.pageList(processOwn,page,rows);
    }

    /**
     * 新增
     * @return
     */
    @RequestMapping("/insert")
    @ResponseBody
    public Map<String,Object> insert(HttpServletRequest request,String departmentId,String time){
        String userName=getSessionUserName(request);
        String userId=getSessionUserId(request);

        ActDeployment deployment=actDeploymentService.findByPdId(departmentId);
        ProcessOwn processOwn=new ProcessOwn();
        processOwn.setUserId(userId);
        processOwn.setUserName(userName);
        Date date=new Date();
        processOwn.setCreateTime(date);
        processOwn.setProcessName(DataUtil.createProcessName(deployment.getDeploymentName(),date,userName));
        processOwn.setDeploymentId(deployment.getDeploymentId());
        processOwn.setPdId(deployment.getDepId());
        processOwn.setPdkey(deployment.getDepKey());

        int i=0;
        //添加数据
        i= processOwnService.insertEntity(processOwn,deployment,userName,userId);
        if(i>=1){
            return successMessage("成功开始一个流程");
        }else{
            return falseMessage("流程创建失败");
        }
    }

    @RequestMapping("/pageListUnaudited")
    @ResponseBody
    public Page<QueryTask> pageListUnaudited(HttpServletRequest request, int page, int rows){
        String userId=getSessionUserId(request);
        return processOwnService.pageListTask(userId,page,rows);
    }

    //审核信息
    @RequestMapping("/auditor")
    @ResponseBody
    public Map<String,Object> auditor(String id,HttpServletRequest request){
        try {
            String userId=getSessionUserId(request);
            processOwnService.auditor(id,userId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return successMessage("成功");
    }
}
